<?php

/**
 * CatgoryController.class.php 
 * Created Time 2016年4月8日 下午4:57:10
 *
 * @author     codingyuan@163.com
 */
namespace Home\Controller\Admin;

use Home\Common\AdminController;

class CatgoryController extends AdminController {
    protected $objServ;
    // 初始化控制器，在具体应用控制器中尽量使用thinkphp提供的初始化工具
    public function _initialize()
    {

        $this->objServ = D('Category', 'Service');
    }

    public function setcat()
    {

        $this->contentTitle = '分类设置';
        $data = array();
        $data = $this->objServ->getCatList('cid,pid,cname,sort');
        $this->__proCatUrl($data);
        $data = $this->objServ->proCatList($data);
        $this->assign('data', $data);
        $this->assign('remote', U('Home/Admin/Catgory/remote@' . C('domain')));
        $this->loadSystemView('setting');
    }

    public function dosubmit()
    {

        try
        {
            if (! IS_POST)
            {throw new \Exception(L('_ERR_FORMART_METHOD_'), array(
                        'method' => 'post' 
                ));}
            $this->objServ->saveData(I('post.'), $id);
            $jumpUrl = U('Home/Admin/Catgory/setcat');
            return $this->success('成功保存', $jumpUrl);
        } catch ( \Exception $e )
        {
            $this->error($e->getMessage());
        }
    }

    public function remote()
    {

        try
        {
            if (! IS_AJAX)
            {throw new \Exception(L('_ERR_FORMART_VISIT_', array(
                        'visit' => 'ajax' 
                )));}
            if ($cid = I('get.cid'))
            {
                $data = $this->objServ->getCatRow($cid, 'pid,sort,cname');
                $data ['cid'] = $cid;
                $this->assign('data', $data);
            }
            $this->assign('act_url', U('Home/Admin/Catgory/dosubmit@' . C('domain')));
            // 获取一级分类
            $catList = $this->objServ->getCatList('cid,cname', true);
            $this->assign('catlist', $catList);
        } catch ( \Exception $e )
        {
            
            return $this->show($e->getMessage());
        }
        
        return $this->display('remote');
    }

    public function del()
    {
        $msg = '';
        $rs = false;
        try
        {
            if (! IS_AJAX)
            {throw new \Exception(L('_ERR_FORMART_VISIT_', array('visit' => 'ajax' )), 100);}
            $cid = I('get.cid');
            if(!$cid)
            {
                throw new \Exception(L('_ERR_FORMART_PARAMS_NUM',array('param' => 'cid')));
            }
            $rs = $this->objServ->delRow($cid);
        } catch ( \Exception $e )
        {
            $msg = $e->getMessage();
            if($e->getCode() == 100)
            {
                return $this->show($msg);
            }
        }
        
        return get_api_result('Home', $rs, $msg);
    }

    private function __proCatUrl(&$data)
    {

        if (! $data)
        {return array();}
        
        foreach ($data as &$v)
        {
            $v ['edit_url'] = U('Home/Admin/Catgory/remote@' . C('domain'), array(
                    'cid' => $v ['cid'] 
            ));
            $v ['del_url'] = U('Home/Admin/Catgory/del@' . C('domain'), array(
                    'cid' => $v ['cid'] 
            ));
        }
        
        return true;
    }

}
 